# 多阶段构建

# 构建阶段
FROM node:20-alpine AS builder

# 设置工作目录
WORKDIR /app

# 复制 package.json 和 package-lock.json
COPY package*.json ./

# 设置npm镜像源并安装依赖
RUN npm config set registry https://registry.npmmirror.com && \
    npm ci || npm ci || npm ci

# 复制源代码
COPY . .

# 构建应用
RUN npm run build

# 生产阶段
FROM nginx:alpine

# 安装 dumb-init
RUN apk add --no-cache dumb-init

# 复制构建结果到 nginx 目录
COPY --from=builder /app/dist /usr/share/nginx/html

# 复制 nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf

# 确保nginx用户权限（用户已存在于基础镜像中）

# 设置权限
RUN chown -R nginx:nginx /usr/share/nginx/html && \
    chmod -R 755 /usr/share/nginx/html

# 暴露端口
EXPOSE 80

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost/ || exit 1

# 使用 dumb-init 作为入口点
ENTRYPOINT ["/usr/bin/dumb-init", "--"]

# 启动 nginx
CMD ["nginx", "-g", "daemon off;"]